<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>回复管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="回复内容">
          <el-input
            v-model="queryInfo.replyText"
            clearable
            @clear="queryList"
            placeholder="请输入回复内容"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toAddReply">新增回复</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="replyText" label="回复内容" width="180"> </el-table-column>
      <el-table-column prop="question.title" label="问题标题" width="180"> </el-table-column>
      <el-table-column prop="user.username" label="用户名称" width="180"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-link type="primary" @click="toEditReply(scope.row)"
              >编辑回复</el-link
            >
          </div>
          <el-link type="primary" @click="deleteReply(scope.row.id)"
            >删除回复</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[1, 5, 10, 15, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- dialog表单对话框 -->
    <el-dialog title="回复" :visible.sync="dialogFormVisible" @close="cancel">
      <el-form :model="reply">
        <el-form-item label="回复内容" :label-width="formLabelWidth">
          <el-input v-model="reply.replyText" autocomplete="off"></el-input>
        </el-form-item>
        
        <el-form-item label="问题标题" :label-width="formLabelWidth">
          <el-select v-model="reply.question_id" filterable placeholder="请选择">
					    <el-option
					      v-for="item in questionList"
					      :key="item.id"
					      :label="item.title"
					      :value="item.id">
					    </el-option>
					  </el-select>
        </el-form-item>

        <el-form-item label="用户名称" :label-width="formLabelWidth">
         <el-select v-model="reply.user_id" filterable placeholder="请选择">
					    <el-option
					      v-for="item in userList"
					      :key="item.id"
					      :label="item.username"
					      :value="item.id">
					    </el-option>
					  </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addReply">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      reply: {
        id: "",
        replyText: "",
        createTime: "",
        question_id: "",
        user_id:"",
        question:{
        },
        user:{}
      },
      questionList:[],
      userList:[],
      total: "",
      dialogFormVisible: false,
      tableData: [],
      queryInfo: {
        replyText: "",
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  methods: {
    // 进入修改的预览数据页面
toEditReply(row) {
	//console.log(row)
	// 赋值
	this.reply = row;
	// 设置标题
	this.title = "修改评论";
	// 开启对话框
	this.dialogFormVisible = true;
},

    deleteReply(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("确认删除");
          this.$http
            .delete("reply/" + id)
            .then((resp) => {
              if (resp.data.status == 200) {
                this.$message.success(resp.data.message);
                this.queryList();
              } else {
                this.$message.error(resp.data.message);
              }
            })
            .catch((error) => {
              console.error("删除失败:", error);
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          console.log("取消删除");
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    addReply() {
      if (this.reply.id) {
        // 有值为true  修改
        // .........后面实现..............
            console.log('11')
        console.log(this.reply)
        console.log('11')
        this.$http.put("reply", this.reply).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("修改成功！");
            // 清空数据
            this.reply = {
             
            };
               this.cancel();
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      } else {
        console.log('11')
        console.log(this.reply)
        console.log('11')
        this.$http.post("reply", this.reply).then((resp) => {
          if (resp.data.status == 200) {
            // 消息提示
            this.$message.success("添加成功！");
            // 清空数据
            this.reply = {
           
            };
            this.cancel();
            // 关闭对话框
            this.dialogFormVisible = false;
            // 重新查询
            this.queryList();
          } else {
            this.$message.error("接口异常！");
          }
        });
      }
    },

    cancel() {
      this.dialogFormVisible = false;
      this.reply = {};
    },
    // toEditReply(row) {
    //   this.reply = row;
    //   this.dialogFormVisible = true;
    // },
    toAddReply() {
      this.dialogFormVisible = true;
    },
    handleSizeChange(size) {
      //console.log('每页显示数量发生改变');
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      // 重新查询
      this.queryList();
    },
    handleCurrentChange(num) {
      //console.log('页码发生改变');
      this.queryInfo.pageNum = num;
      // 重新查询
      this.queryList();
    },
    queryList() {
      // 调用后端接口来获取数据
      this.$http
        .get("/reply", {
          params: this.queryInfo,
        })
        .then((resp) => {
          if (resp.data.status == 200) {
            console.log(resp.data);
            this.tableData = resp.data.data.list;
            this.total = resp.data.data.total;
            console.log(this.tableData);
          } else {
            this.$message.error("查询失败！");
          }
        });
    },
    queryQuestion(){
            this.$http.get('/question/findAll')
            .then((resp) => {
          if (resp.data.status == 200) {
            this.questionList = resp.data.data;
            this.total = resp.data.data.total;
          } else {
            this.$message.error("查询失败！");
          }
        });
    },
    queryUser(){
         this.$http.get('/User/findAll')
            .then((resp) => {
          if (resp.data.status == 200) {
            this.userList = resp.data.data;
            this.total = resp.data.data.total;
          } else {
            this.$message.error("查询失败！");
          }
        });
    }
  },
  mounted() {
    this.queryList();
    this.queryQuestion();
    console.log(this.questionList)
    this.queryUser();
  },
};
</script>
<style >
</style>